package com.brillilab.service.core.kit.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.kit.ReagentPrimerAttrMapper;
import com.brillilab.dao.mapper.kit.ReagentPrimerMapper;
import com.brillilab.domain.po.kit.ReagentPrimer;
import com.brillilab.domain.po.kit.ReagentPrimerAttr;
import com.brillilab.service.core.kit.IReagentPrimerService;
@Service
@Transactional
public class ReagentPrimerServiceImpl implements IReagentPrimerService{
	
	@Autowired
	private ReagentPrimerMapper reagentPrimerMapper;	
	@Autowired
	private ReagentPrimerAttrMapper reagentPrimerAttrMapper;	

	@Override
	public ReagentPrimer insert(ReagentPrimer entity) {
		Integer insert = reagentPrimerMapper.insert(entity);
		Assert.isTrue(insert==1, "添加失败");
		return entity;
	}
	
	@Override
	public boolean deleteById(Long id) {
		Integer b = reagentPrimerMapper.deleteById(id);
		return b==1 ? true: false;
	}
	
	@Override
	public boolean updateById(ReagentPrimer entity) {
		entity.setUpdateTime(new Date());
		Integer b = reagentPrimerMapper.updateById(entity);
		return b==1 ? true: false;
	}
	
	@Override
	public ReagentPrimer selectById(Long id) {
		return reagentPrimerMapper.selectById(id);
	}

	@Override
	public ReagentPrimer selectOne(Long kitId, Long reagentId) {
		LambdaQueryWrapper<ReagentPrimer> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(ReagentPrimer::getKitId,kitId);
		wrapper.eq(ReagentPrimer::getReagentId,reagentId);
		ReagentPrimer reagentPrimer = reagentPrimerMapper.selectOne(wrapper);
		if(reagentPrimer!=null){
			LambdaQueryWrapper<ReagentPrimerAttr> attrWrapper = new LambdaQueryWrapper<>();
			attrWrapper.eq(ReagentPrimerAttr::getKitId,kitId);
			attrWrapper.eq(ReagentPrimerAttr::getReagentId,reagentId);
			List<ReagentPrimerAttr> primerAttrList = reagentPrimerAttrMapper.selectList(attrWrapper);
			reagentPrimer.setPrimerAttrList(primerAttrList);
		}
		return reagentPrimer;
	}
}
